/**
 * Created by shc@lierda.com on 2017/4/15.
 */
var DB =  require('../db/db');
var IncreaseId = require('../../web/tools/IncreaseId');

/**获取banner信息*/
exports.getBanner=function(req,res,msg){
    var id = req.body.id;
    if(id){
        var sql="SELECT banner_name as name,banner_top as topImg from k_banner_goods WHERE id ='"+id+"'";
    }else{
        var sql="SELECT * from k_banner_goods";
    }
    console.log(sql);
    DB.query(sql,function(er,list){
        if(!er){
            msg.success=true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};

/**获取店铺功能信息*/
exports.getFunction=function(req,res,msg){
    var tn = req.body.tn;
    if(tn){
        var sql="SELECT * from k_black WHERE table_name ='"+tn+"'";
    }else{
        var sql="SELECT * from k_black";
    }
    console.log(sql);
    DB.query(sql,function(er,list){
        if(!er){
            msg.success=true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};

/**根据barcode获取goods_id*/
exports.getGoodsIdByBarcode= function(req,res,msg){
    var  barcode = req.body.barcode;
    if(barcode){
        var sql="SELECT goods_id from k_goods where barcode ='"+barcode+"'";
        console.log(sql);
        DB.query(sql,function(er,list){
            if(!er){
                msg.success=true;
                msg.data = list;
                res.json(msg);
            }else{
                msg.msg=er;
                res.json(msg);
            }
        });
    }else{
        msg.msg="参数有误";
        res.json(msg);
    }
};

/**根据uid获取个人信息*/
exports.getUserInfoByUid= function(req,res,msg){
    var  userId= req.body.userId;
    if(userId){
        var sql="SELECT * from k_user where id='"+userId+"'";
        console.log(sql);
        DB.query(sql,function(er,list){
            if(!er){
                msg.success=true;
                msg.data = list;
                res.json(msg);
            }else{
                msg.msg=er;
                res.json(msg);
            }
        });
    }else{
        msg.msg="参数有误";
        res.json(msg);
    }
};



/**根据shopid获取店铺信息*/
exports.getShopInfoByShopid=function(req,res,msg){
    var id = req.body.id;
    if(id){
        var sql="SELECT * from k_shop where id='"+id+"'";
        console.log(sql);
        DB.query(sql,function(er,list){
            if(!er){
                msg.success=true;
                msg.data = list;
                res.json(msg);
            }else{
                msg.msg=er;
                res.json(msg);
            }
        });
    }else{
        msg.msg="参数有误";
        res.json(msg);
    }
};
/**根据uid获取收货地址*/
exports.getAddressByUid= function(req,res,msg){
    var Userid = req.body.uid;
    if(Userid){
        var sql="SELECT * from k_address where uid='"+Userid+"'";
        //console.log(sql);
        DB.query(sql,function(er,list){
            if(!er){
                msg.success=true;
                msg.data = list;
                res.json(msg);
            }else{
                msg.msg=er;
                res.json(msg);
            }
        });
    }else{
        msg.msg="参数有误";
        res.json(msg);
    }
};

//新建or修改收货地址 添加到数据库
exports.changeAddress=function(req,res,msg){
    var id = req.body.id;
    var uid = req.body.uid;
    var realname = req.body.realname;
    var mobile = req.body.mobile;
    var province = req.body.province;
    var city = req.body.city;
    var district = req.body.district;
    var idnumber = req.body.idnumber;
    var address = req.body.address;
    var zipcode = req.body.zipcode;
    var isdefault = req.body.isdefault;
    if(id==0){
        //新建地址
        id = IncreaseId.increaseId();
        var sql="SELECT * from k_address where uid='"+uid+"'";
        console.log(sql);
        DB.query(sql,function(er,list){
            if(!er&&list.length>0){
                if(isdefault == 0){
                    var sql2 = "INSERT INTO k_address(id,uid,realname,mobile,province,city,district,idnumber,address,zipcode,isdefault) VALUES ('"
                        +id+"','"
                        +uid+"','"
                        +realname+"','"
                        +mobile+"','"
                        +province+"','"
                        +city+"','"
                        +district+"','"
                        +idnumber+"','"
                        +address+"','"
                        +zipcode+"','"
                        +isdefault+"')";
                    console.log(sql2);
                    DB.query(sql2,function(er,list){
                        if(!er){
                            msg.success=true;
                            msg.data = list;
                            res.json(msg);
                        }else{
                            msg.msg=er;
                            res.json(msg);
                        }
                    });
                }else{
                    var sql1 = "UPDATE k_address SET isdefault = 0 WHERE uid ='" + uid + "'";
                    console.log(sql1);
                    DB.query(sql1, function (er, list) {
                        if (!er) {
                            var sql2 = "INSERT INTO k_address(id,uid,realname,mobile,province,city,district,idnumber,address,zipcode,isdefault) VALUES ('"
                                +id+"','"
                                +uid+"','"
                                +realname+"','"
                                +mobile+"','"
                                +province+"','"
                                +city+"','"
                                +district+"','"
                                +idnumber+"','"
                                +address+"','"
                                +zipcode+"','"
                                +isdefault+"')";
                            console.log(sql2);
                            DB.query(sql2,function(er,list){
                                if(!er){
                                    msg.success=true;
                                    msg.data = list;
                                    res.json(msg);
                                }else{
                                    msg.msg=er;
                                    res.json(msg);
                                }
                            });
                        }
                    });
                }
            }else{
                var sql2 = "INSERT INTO k_address(id,uid,realname,mobile,province,city,district,idnumber,address,zipcode,isdefault) VALUES ('"
                    +id+"','"
                    +uid+"','"
                    +realname+"','"
                    +mobile+"','"
                    +province+"','"
                    +city+"','"
                    +district+"','"
                    +idnumber+"','"
                    +address+"','"
                    +zipcode+"','"
                    +isdefault+"')";
                console.log(sql2);
                DB.query(sql2,function(er,list){
                    if(!er){
                        msg.success=true;
                        msg.data = list;
                        res.json(msg);
                    }else{
                        msg.msg=er;
                        res.json(msg);
                    }
                });
            }
        });
    }else {
        //修改地址
        if (isdefault == 1) {
            var sql = "UPDATE k_address SET isdefault = 0 WHERE uid ='" + uid + "'";
            console.log(sql);
            DB.query(sql, function (er, list) {
                if (!er) {
                    var sql2 = "UPDATE k_address SET "
                        + "realname ='" + realname + "',"
                        + "mobile ='" + mobile + "',"
                        + "province ='" + province + "',"
                        + "city ='" + city + "',"
                        + "district ='" + district + "',"
                        + "idnumber ='" + idnumber + "',"
                        + "address ='" + address + "',"
                        + "zipcode ='" + zipcode + "',"
                        + "isdefault ='" + isdefault + "'"
                        + " WHERE id ='" + id + "' AND uid ='"+uid+"'";
                    console.log(sql2);
                    DB.query(sql2, function (er, list) {
                        if (!er) {
                            msg.success = true;
                            msg.msg = "更新默认地址";
                            console.log(msg);
                            res.json(msg);
                        } else {
                            msg.msg = er;
                            res.json(msg);
                        }
                    });
                } else {
                    msg.msg = er;
                    res.json(msg);
                }
            });
            console.log(isdefault);

        }else {
            var sql3 = "UPDATE k_address SET "
                + "realname ='" + realname + "',"
                + "mobile ='" + mobile + "',"
                + "province ='" + province + "',"
                + "city ='" + city + "',"
                + "district ='" + district + "',"
                + "idnumber ='" + idnumber + "',"
                + "address ='" + address + "',"
                + "zipcode ='" + zipcode + "',"
                + "isdefault ='" + isdefault + "'"
                + " WHERE id ='" + id + "' AND uid ='"+uid+"'";
            console.log(sql3);
            DB.query(sql3, function (er, list) {
                if (!er) {
                    msg.success = true;
                    msg.msg = "更新默认地址";
                    res.json(msg);
                } else {
                    msg.msg = er;
                    res.json(msg);
                }
            });
        }
    }
}

//delete 删除地址
exports.deleteAddressById=function(req,res,msg){
    var AddressId = req.body.AddressId;
    var uid = req.body.uid;
    if(AddressId){
        var sql = "DELETE FROM k_address WHERE id ='" +AddressId+"' AND uid ='"+uid+"'";
        console.log(sql);
        DB.query(sql,function(er,list){
            if(!er){
                msg.success=true;
                msg.data = list;
                res.json(msg);
            }else{
                msg.msg=er;
                res.json(msg);
            }
        });
    }else{
        msg.msg="参数有误";
        res.json(msg);
    }
}

/**获取发现信息*/
exports.getFaxian=function(req,res,msg){
    var sql="SELECT * from k_discover";
    console.log(sql);
    DB.query(sql,function(er,list){
        if(!er){
            msg.success=true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};
/**获取一人一物信息*/
exports.getRenwu = function(req,res,msg){
    var sql="SELECT * FROM k_goods_person";
    console.log(sql);
    DB.query(sql,function(er,list){
        if(!er){
            msg.success=true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};
